package info.rangier;

import info.help.MMisc;
import javax.swing.DefaultListModel;

public class RangierFrame extends javax.swing.JFrame {

    Stapel sp1;
    Stapel sp2;
    Stapel sp3;
    boolean dirUp = true;

    public RangierFrame() {
        initComponents();
        bnClearActionPerformed(null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lA = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        lB = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        lC = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        bnFillA = new javax.swing.JButton();
        spinCA = new javax.swing.JSpinner();
        bnSort = new javax.swing.JButton();
        bnClear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Rangier Dingens");

        lA.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lA);

        lB.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(lB);

        lC.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(lC);

        jLabel2.setText("A");

        jLabel3.setText("B");

        jLabel4.setText("C");

        bnFillA.setText("Fülle A");
        bnFillA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnFillAActionPerformed(evt);
            }
        });

        spinCA.setToolTipText("Anzahl zum Füllen");
        spinCA.setAutoscrolls(true);
        spinCA.setValue(6);

        bnSort.setText("Rangieren");
        bnSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnSortActionPerformed(evt);
            }
        });

        bnClear.setText("Zurücksetzen");
        bnClear.setToolTipText("");
        bnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(spinCA)
                    .addComponent(bnFillA, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addComponent(jLabel2))
                .addGap(149, 149, 149)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(bnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(bnSort, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bnFillA)
                    .addComponent(bnSort)
                    .addComponent(bnClear))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spinCA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                    .addComponent(jScrollPane3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4)))
                .addGap(13, 13, 13))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bnFillAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnFillAActionPerformed
        DefaultListModel m = new DefaultListModel();
        for (int i = 0; i < (int) spinCA.getValue(); i++) {
            int zahl = MMisc.zufall(1, 99);
            m.addElement(zahl);
            sp1.push(zahl);

        }
        lA.setModel(m);
    }//GEN-LAST:event_bnFillAActionPerformed

    private void bnSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnSortActionPerformed
        if (!sp1.isEmpty() && sp3.isEmpty()) {
            while (!sp1.isEmpty()) {
                int a = (int) sp1.top();
                if (!sp3.isEmpty()) {
                    int c = (int) sp3.top();
                    if (dirUp) {
                        if (a < c) {
                            moveFirstTo(3, 2);
                            moveFirstTo(1,3);
                            moveFirstTo(2, 3);
                        } else {
                            moveFirstTo(1, 3);
                        }
                    } else {

                    }
                } else {
                    moveFirstTo(1, 3);
                }
                
            }
        }
        updateA();
        updateB();
        updateC();
    }//GEN-LAST:event_bnSortActionPerformed

    private void bnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnClearActionPerformed
        sp1 = new Stapel();
        sp2 = new Stapel();
        sp3 = new Stapel();
        bnFillAActionPerformed(null);
        DefaultListModel m = new DefaultListModel();
        lB.setModel(m);
        lC.setModel(m);
    }//GEN-LAST:event_bnClearActionPerformed

    private void updateA() {
        DefaultListModel m = new DefaultListModel();
        Stapel temp = (Stapel) sp1.clone();

        while (!temp.isEmpty()) {
            m.addElement(temp.top());
            temp.pop();
        }
        lA.setModel(m);
    }

    private void updateB() {
        DefaultListModel m = new DefaultListModel();
        Stapel temp = (Stapel) sp2.clone();

        while (!temp.isEmpty()) {
            m.addElement(temp.top());
            temp.pop();
        }
        lB.setModel(m);
    }

    private void updateC() {
        DefaultListModel m = new DefaultListModel();
        Stapel temp = (Stapel) sp3.clone();

        while (!temp.isEmpty()) {
            m.addElement(temp.top());
            temp.pop();
        }
        lC.setModel(m);
    }

    public void moveFirstTo(int a, int b) {
        switch (a) {
            case 1:
                switch (b) {
                    case 1:
                        break;
                    case 2:
                        sp1.transferFirst(sp2);
                        break;
                    case 3:
                        sp1.transferFirst(sp3);
                        break;
                }
                break;
            case 2:
                switch (b) {
                    case 1:
                        sp2.transferFirst(sp1);
                        break;
                    case 2:
                        break;
                    case 3:
                        sp2.transferFirst(sp3);
                        break;
                }
                break;
            case 3:
                switch (b) {
                    case 1:
                        sp3.transferFirst(sp1);
                        break;
                    case 2:
                        sp3.transferFirst(sp2);
                        break;
                    case 3:
                        break;
                }
                break;
        }

    }

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RangierFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RangierFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RangierFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RangierFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RangierFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bnClear;
    private javax.swing.JButton bnFillA;
    private javax.swing.JButton bnSort;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList lA;
    private javax.swing.JList lB;
    private javax.swing.JList lC;
    private javax.swing.JSpinner spinCA;
    // End of variables declaration//GEN-END:variables
}
